﻿Imports USA411
Imports USA411.U3Config
Imports System.Data.OleDb

Public Class PortConfig
    Public frmAction As New ActionConfig
   
    Friend Sub RefreshForm()
        With U3Cfg(EditDevice).Tables(0).Rows(EditPort)
            lblSerialNumber.Text = .Item("DeviceID")
            lblPortID.Text = CType(.Item("PortID"), dPorts).ToString
            If .Item("PortType") = "" Then .Item("PortType") = "D"
            If .Item("PortType") = "D" Then
                btnPortType.Text = "Digital"
            Else
                btnPortType.Text = "Analogue"
            End If
            If .Item("Direction") = "" Then .Item("Direction") = "I "
            If Mid(.Item("Direction"), 1, 1) = "I" Then
                cbIn.Checked = True
            Else
                cbIn.Checked = False
            End If
            If Mid(.Item("Direction"), 2, 1) = "O" Then
                cbOut.Checked = True
            Else
                cbOut.Checked = False
            End If
            numDebounce.Value = .Item("DebounceCount")
            numSkipCount.Value = .Item("SkipCount")
            If .Item("AlarmState") Then
                btnAlarmState.Text = "Closed/True"
            Else
                btnAlarmState.Text = "Open/False"
            End If
            txtDescription.Text = .Item("Description")
            cbAlarmActions.SelectedValue = .Item("AlarmActionID")
            cbSafeActions.SelectedValue = .Item("SafeActionID")
           
        End With

    End Sub

    Private Sub PortConfig_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub

    Private Sub PortConfig_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        cbAlarmActions.DataSource = a1
        cbAlarmActions.DisplayMember = "Description"
        cbAlarmActions.ValueMember = "ActionID"
        cbSafeActions.DataSource = a2
        cbSafeActions.DisplayMember = "Description"
        cbSafeActions.ValueMember = "ActionID"


        RefreshForm()
    End Sub

#Region "Change Events"


    Private Sub btnPortType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPortType.Click
        If btnPortType.Text = "Digital" Then
            btnPortType.Text = "Analogue"
            U3Cfg(EditDevice).Tables(0).Rows(EditPort).Item("PortType") = "A"
        Else
            btnPortType.Text = "Digital"
            U3Cfg(EditDevice).Tables(0).Rows(EditPort).Item("PortType") = "A"
        End If
    End Sub

    Private Sub cbIn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbIn.CheckedChanged
        If cbIn.Checked Then
            Mid(U3Cfg(EditDevice).Tables(0).Rows(EditPort).Item("Direction"), 1, 1) = "I"
        Else
            Mid(U3Cfg(EditDevice).Tables(0).Rows(EditPort).Item("Direction"), 1, 1) = " "
         End If
        SetActive()
    End Sub
    Private Sub cbOut_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbOut.CheckedChanged
        If cbOut.Checked Then
            Mid(U3Cfg(EditDevice).Tables(0).Rows(EditPort).Item("Direction"), 2, 1) = "O"
        Else
            Mid(U3Cfg(EditDevice).Tables(0).Rows(EditPort).Item("Direction"), 2, 1) = " "
        End If
        SetActive()
    End Sub

    Private Sub btnAlarmState_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAlarmState.Click
        If btnAlarmState.Text = "Open/False" Then
            btnAlarmState.Text = "Closed/True"
            U3Cfg(EditDevice).Tables(0).Rows(EditPort).Item("AlarmState") = True
        Else
            btnAlarmState.Text = "Open/False"
            U3Cfg(EditDevice).Tables(0).Rows(EditPort).Item("AlarmState") = False
        End If
    End Sub

    Private Sub numDebounce_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numDebounce.ValueChanged
        U3Cfg(EditDevice).Tables(0).Rows(EditPort).Item("DebounceCount") = numDebounce.Value
    End Sub

    Private Sub numSkipCount_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numSkipCount.ValueChanged
        U3Cfg(EditDevice).Tables(0).Rows(EditPort).Item("SkipCount") = numSkipCount.Value
    End Sub

    'Private Sub txtHSCode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    _Config.Item("HSCode") = txtHSCode.Text
    'End Sub

    Private Sub txtDescription_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDescription.TextChanged
        U3Cfg(EditDevice).Tables(0).Rows(EditPort).Item("Description") = txtDescription.Text
    End Sub
#End Region

    Private Sub SetActive()
        With U3Cfg(EditDevice).Tables(0).Rows(EditPort)
            If .Item("Direction") <> "  " Then
                pnlActive.BackColor = Color.LightGreen
                .Item("Active") = True
            Else
                pnlActive.BackColor = Color.Red
                .Item("Active") = False
            End If
        End With

    End Sub

  
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Hide()
    End Sub


    Private Sub btnUpdatePort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdatePort.Click
        With U3Cfg(EditDevice).Tables(0).Rows(EditPort)


        End With


        WritePorts()

        'Me.Hide()
    End Sub




    Private Sub btnAddEditActions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddEditActions.Click
        With frmAction
            .Show()
            .TopMost = True
        End With
    End Sub

    Private Sub cbAlarmActions_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbAlarmActions.SelectedIndexChanged
        U3Cfg(EditDevice).Tables(0).Rows(EditPort).Item("AlarmActionID") = cbAlarmActions.SelectedValue
    End Sub

    Private Sub cbSafeActions_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbSafeActions.SelectedIndexChanged
        U3Cfg(EditDevice).Tables(0).Rows(EditPort).Item("SafeActionID") = cbSafeActions.SelectedValue
    End Sub
End Class